package com.example.common.dto;
/**
 * @description: 统一返回类
 * @date: 2021/9/9
 */

import com.example.common.enums.ReturnCodeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

@Data
@AllArgsConstructor
public class BaseReturnDto<T> implements Serializable {

    // 返回码
    private int code;

    // 返回码说明
    private String msg;

    //数据
    private T data;

    public BaseReturnDto() {
    }

    public BaseReturnDto(ReturnCodeEnum returnCode) {
        this.code = returnCode.getCode();
        this.msg = returnCode.getMsg();
    }

    public BaseReturnDto(ReturnCodeEnum returnCodeEnum, String msg) {
        this.code = returnCodeEnum.getCode();
        this.msg = returnCodeEnum.getMsg();
    }

    public BaseReturnDto(ReturnCodeEnum returnCodeEnum, String msg, T data) {
        this.code = returnCodeEnum.getCode();
        this.msg = returnCodeEnum.getMsg();
        this.data = data;
    }

    public BaseReturnDto(ReturnCodeEnum returnCodeEnum, T data) {
        this.code = returnCodeEnum.getCode();
        this.msg = returnCodeEnum.getMsg();
        this.data = data;
    }

    public com.example.common.dto.BaseReturnDto data(T data){
        this.setData(data);
        return this;
    }

    public com.example.common.dto.BaseReturnDto code(int code){
        this.setCode(code);
        return this;
    }

    public com.example.common.dto.BaseReturnDto code(ReturnCodeEnum code){
        this.setCode(code.getCode());
        return this;
    }

    public com.example.common.dto.BaseReturnDto msg(String msg){
        this.setMsg(msg);
        return this;
    }

    /**
     * 无数据返回成功
     * @return
     */
    public static com.example.common.dto.BaseReturnDto success() {
        com.example.common.dto.BaseReturnDto result = new com.example.common.dto.BaseReturnDto();
        result.setCode(ReturnCodeEnum.SUCCESS.getCode());
        result.setMsg(ReturnCodeEnum.SUCCESS.getMsg());
        return result;
    }

    /**
     * 无数据返回失败
     * @return
     */
    public static com.example.common.dto.BaseReturnDto error() {
        com.example.common.dto.BaseReturnDto result = new com.example.common.dto.BaseReturnDto();
        result.setCode(ReturnCodeEnum.PARAMS_ERROR.getCode());
        result.setMsg(ReturnCodeEnum.PARAMS_ERROR.getMsg());
        return result;
    }
}
